package day01;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;

public class MyArrayList {
    public static void main(String[] args) {
        //1.创建ArrayList对象
        //底层已赋值空数组
        ArrayList<String> list = new ArrayList<String>();
        //2.增
        list.add("A");
        list.add(0,"B");
        //3.查
        System.out.println(list.get(1));//A
        //4.改
        list.set(1,"C");
        System.out.println(list.get(1));//C
        //5.删
        list.remove(1);
        System.out.println(list);//[B]
        //增加元素
        list.add(1,"C");
        list.add(2,"D");

        //遍历
        //1.for
        System.out.println("------------------111-------------------");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i));
        }//B,C,D
        System.out.println("");
        //2.增强for
        System.out.println("------------------222-------------------");
        for(String s : list) {
            System.out.print(s);
        }
        System.out.println("");
        //3.迭代器
        System.out.println("------------------333-------------------");
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()) {
            System.out.print(iterator.next());
        }
        System.out.println("");
        //4.lambda表达的方式
        System.out.println("------------------444-------------------");
//        Consumer<String> consumer =System.out::println;
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.print(s);
            }
        });
        System.out.println("");
        System.out.println("------------------555-------------------");
       list.forEach(name->System.out.print(name));
        System.out.println("");
        System.out.println("------------------666-------------------");
       list.forEach(System.out::print);
    }
}
